from django.db import models
from model import BaseModel


# Create your models here.

class Category(BaseModel):
    cname = models.CharField(max_length=50, verbose_name="分类名称")
    parent = models.ForeignKey("self", null=True, blank=True, on_delete=models.CASCADE, verbose_name="上级分类",
                               related_name="children")
    ishome = models.BooleanField(default=False, verbose_name="是否首页显示")
    isgood = models.BooleanField(default=False, verbose_name="是否精品课程分类")

    class Meta:
        db_table = "fg_category"
        verbose_name = "课程分类"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.cname


class Teacher(BaseModel):
    name = models.CharField(max_length=50, verbose_name='教师名称')
    img = models.CharField(max_length=150, verbose_name='教师头像')
    work = models.CharField(max_length=50, verbose_name='职位')
    desc = models.TextField(verbose_name='个人简介')

    class Meta:
        verbose_name = '教师'
        db_table = 'fg_teacher'

    def __str__(self):
        return self.name


class Course(BaseModel):
    coursename = models.CharField(max_length=50, verbose_name='课程名称')
    coursedesc = models.CharField(max_length=150, verbose_name='课程描述')
    content = models.TextField(verbose_name='课程内容简介')
    price = models.DecimalField(verbose_name="课程价格", max_digits=7, decimal_places=2)
    cate = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name='课程分类')
    level = models.IntegerField(choices=((0, "零基础"), (1, "初级"), (2, "高级")), default=0, verbose_name='难度')
    is_hot = models.BooleanField(default=True, verbose_name="是否为热门")
    is_home = models.BooleanField(default=True, verbose_name="是否推荐到首页")
    cover = models.CharField(max_length=200, verbose_name='课程页面图片')
    timeline = models.IntegerField(default=0, verbose_name='时长')
    students = models.IntegerField(default=0, verbose_name='学习人数')
    video = models.CharField(max_length=200, verbose_name='宣传视频')
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE, verbose_name='当前课程的老师')

    class Meta:
        db_table = "fg_course"

    def __str__(self):
        return self.coursename


class Activity(models.Model):
    aname = models.CharField(max_length=150, verbose_name='活动名称')
    start_time = models.DateTimeField(verbose_name='活动开始时间')
    end_time = models.DateTimeField(verbose_name='活动结束时间')
    activity_desc = models.CharField(max_length=200, verbose_name='活动描述')
    type = models.IntegerField(choices=((0, "折扣"), (1, "价格")), default=1, verbose_name='活动类型')

    class Meta:
        db_table = "fg_activity"

    def __str__(self):
        return self.aname


class ActivityCourse(models.Model):
    activity = models.ForeignKey(Activity, on_delete=models.CASCADE, verbose_name='活动')
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name='课程')
    discount = models.DecimalField(max_digits=9, decimal_places=2, verbose_name='折扣')

    class Meta:
        db_table = "fg_activity_course"
